﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;




namespace VoliaStat
{
    public class VoiaInfoParser
    {

        //const string NOINET = "stat.voliacable.com";
        const string NOINET = "stat.volia.com";

        IVoliaInfo _info;
        int _totalid=2; // 0
        int _monthid=0;  // 1
        int _dayid=1;   // 2
        int _tvid=1;    // 3
        int _inetid=2;  // 4


        public VoiaInfoParser(IVoliaInfo info)
        {
            _info = info;
            Process();

        }



        void Process()
        {
            int i;
            bool rc;
            string s;

            i=0;
            while (true)
            {
                rc = _info.Get(string.Format("traffic.{0}.range",i),out s);
                if (!rc) break;
                if (s=="month") _monthid=i;
                if (s=="day") _dayid=i;
                i++;
            }

            i=0;
            while (true)
            {
                rc = _info.Get(string.Format("traffic.0.rows.{0}.direction",i),out s);
                if (!rc) break;
                if (s=="Всего") _totalid=i;
                i++;
            }

            i=0;
            while (true)
            {
                rc = _info.Get(string.Format("services.{0}.code",i),out s);
                if (!rc) break;
                if (s=="dg_cable") _tvid=i;
                if (s=="inet") _inetid=i;
                i++;
            }
        }



        string Str(string template)
        {
            return string.Format(template,_totalid,_monthid,_dayid,_tvid,_inetid);
        }

        string GetStr(string template)
        {
            string v;
            _info.Get(Str(template),out v);
            return v;

        }

        long GetInt(string template)
        {
            long v;
            _info.Get(Str(template),out v);
            return v;
        }













        public bool IsError
        {
            get
            {
                if (_info.Get("Error0.Message")) return true;
                long rc = 0;
                _info.Get("responseStatus.responseCode",out rc);
                return  (rc!=0);
            }
        
        
        }

        public bool IsNoInetError
        {   get
            {
                return ( this.Error.IndexOf(NOINET) != -1 );
            }
        }


        public string Error
        {
            get
            {
                string s;
                if (_info.Get("Error0.Message",out s)) return s;
                return GetStr("responseStatus.responseValue");
            }
        }


        public DateTime Time
        {
            get
            {
                string s = GetStr("timestamp");
                DateTime t = DateTime.Now;
                try
                {
                    t = DateTime.Parse(s);
                } 
                catch{}
                return t;
            }
        }


        public long TraffTotalInt
        {   get
            {
                 return GetInt("traffic.{1}.rows.{0}.traffic");
            }
        }

        public long TraffTodayInt
        {   get
            {
                return GetInt("traffic.{2}.rows.{0}.traffic");
            }
        }



        string TraffToString(long traff)
        {
            double f = ((double)traff)/1000000;
            return  string.Format("{0:0,0.00}",f);
        }

        public string TraffTotal
        {   get
            {
                //string s = GetStr("traffic.{1}.rows.{0}.traffic");
                //if (s.Length!=0) 
                return TraffToString(TraffTotalInt);
                //return s;
            }
        }

        public string TraffToday
        {   get
            {
                string s = GetStr("traffic.{2}.rows.{0}.traffic");
                if (s.Length!=0) return TraffToString(TraffTodayInt);
                return s;
            }
        }

        public long TraffOver
        {   get
            {
                return GetInt("traffic.{1}.rows.{0}.overTraffic");

            }
        }


        public string TraffOverMoney
        {   get
            {

                string s = GetStr("traffic.{1}.rows.{0}.additionalCost");
                if (s.Length!=0) s = "-"+s+" грн.";
                return s;
                
            }
        }



        public string TVName
        {   get
            {
                return GetStr("services.{3}.model");
            }
        }

        public string TVMoney
        {   get
            {
                return GetStr("services.{3}.balans");
            }
        }

        public string INETName
       {   get
            {
                return GetStr("services.{4}.model");
            }
        }

        public string INETMoney
       {   get
            {
                return GetStr("services.{4}.balans");
            }
        }


    }
}
